//
//  AMPSADJGInterstitialAdapter.m
//  AdScopeDemo
//
//  Created by apple on 2025/5/22.
//

#import "AMPSADJGInterstitialAdapter.h"
#import <ADJgSDK/ADJgSDKIntertitialAd.h>
#import <AdScopeFoundation/AdScopeFoundation.h>
#import "AMPSADJGSDKManagerAdapter.h"

@interface AMPSADJGInterstitialAdapter () <ADJgSDKIntertitialAdDelegate>

@property (nonatomic, strong) ADJgSDKIntertitialAd *interstitial;

@property (nonatomic, assign) BOOL isLoadFailHasCallBack;

@property (nonatomic, assign) BOOL isBiddingRequest;

@property (nonatomic, strong) AdScopeTimer *timeoutTimer;

@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@end

@implementation AMPSADJGInterstitialAdapter

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(nonnull NSString *)spaceId adConfiguration:(nonnull AMPSAdConfiguration *)adConfiguration {
    if (AMPSADJGSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [AMPSADJGSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(interstitialAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate interstitialAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)gdtLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.interstitial = [ADJgSDKIntertitialAd new];
    self.interstitial.controller = adConfiguration.viewController;
    self.interstitial.posId = spaceId;
    self.interstitial.delegate = self;
    self.adConfiguration = adConfiguration;
    if (adConfiguration.videoSound == 1) {
        self.interstitial.isMuted = NO;
    } else {
        self.interstitial.isMuted = YES;
    }
    CGFloat floatValue = adConfiguration.timeoutInterval/1000 > 0 ? adConfiguration.timeoutInterval/1000 : 5;
    self.timeoutTimer = [AdScopeTimer adScopeScheduledWeakTimerWithTimeInterval:floatValue target:self selector:@selector(timeoutTimerSelector) repeats:NO];
    [self.interstitial loadAdData];
}

- (void)timeoutTimerSelector {
//    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:-9999 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGInterstitialAdapter load ad timeout"}];
    [self adjg_interstitialAdFailedToLoad:self.interstitial error:[ADJgAdapterErrorDefine new]];
}

- (void)removeAd {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    if (_interstitial) {
        _interstitial.delegate = nil;
        _interstitial = nil;
    }
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;
}

- (void)showInterstitialAdInViewController:(nonnull UIViewController *)viewController {
    [self.interstitial show];
}

- (void)sendLossNotificationWithInfo:(nonnull NSDictionary *)lossInfo {
    
}

- (void)sendWinNotificationWithInfo:(nonnull NSDictionary *)winInfo {
    
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadSuccess:ext:)]) {
            [self.customDelegate interstitialAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    ADJgSDKExtInfo *extInfo = [self.interstitial adjg_extInfo];
    return [extInfo.ecpm integerValue];
}

- (void)adjg_interstitialAdSuccedToLoad:(ADJgSDKIntertitialAd *)interstitialAd {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    self.isLoadAdSuccess = YES;
    if (!self.isBiddingRequest) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadSuccess:ext:)]) {
            [self.customDelegate interstitialAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
        return;
    }
    ADJgSDKExtInfo *extInfo = [self.interstitial adjg_extInfo];
    if ([extInfo.ecpm integerValue] > 0) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGInterstitialAdapter ad bidding fail,ecpm less than 0"}];
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    }
}

- (void)adjg_interstitialAdFailedToLoad:(ADJgSDKIntertitialAd *)interstitialAd error:(ADJgAdapterErrorDefine *)error{
    NSError *tempError = ADJgAdapterError(ADJgAdapterErrorDomain, error.code, error.message);
    if (!_isLoadFailHasCallBack) {
        _isLoadFailHasCallBack = YES;
        if (self.isBiddingRequest) {
            if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                [self.bidDelegate requestBiddingPriceFail:self error:tempError ext:self.adConfiguration.ext];
            }
        } else {
            if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdLoadFail:error:ext:)]) {
                [self.customDelegate interstitialAdLoadFail:self error:tempError ext:self.adConfiguration.ext];
            }
        }
        [self removeAd];
    }
}

- (void)adjg_interstitialAdDidPresent:(ADJgSDKIntertitialAd *)interstitialAd {
    
}

- (void)adjg_interstitialAdExposure:(nonnull ADJgSDKIntertitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidShow:)]) {
        [self.customDelegate interstitialAdDidShow:self];
    }
}

- (void)adjg_interstitialAdFailedToPresent:(ADJgSDKIntertitialAd *)interstitialAd error:(NSError *)error {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdShowFail:error:ext:)]) {
        [self.customDelegate interstitialAdShowFail:self error:error ext:self.adConfiguration.ext];
    }
    [self removeAd];
}

- (void)adjg_interstitialAdDidClose:(ADJgSDKIntertitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidClose:)]) {
        [self.customDelegate interstitialAdDidClose:self];
    }
    [self removeAd];
}

- (void)adjg_interstitialAdDidClick:(ADJgSDKIntertitialAd *)interstitialAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(interstitialAdDidClick:)]) {
        [self.customDelegate interstitialAdDidClick:self];
    }
}

- (void)adjg_interstitialAdCloseLandingPage:(nonnull ADJgSDKIntertitialAd *)interstitialAd { 
    
}

- (void)dealloc {
    [self removeAd];
}

@end
